package com.lucker.framework.exception;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.lucker.framework.constant.ResponseBaseModel;

@ControllerAdvice
@ResponseBody
public class ControllerExceptionHandler {

   private static Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);
	
   @ResponseStatus(HttpStatus.BAD_REQUEST)
   @ExceptionHandler(HttpMessageNotReadableException.class)
   public @ResponseBody ResponseEntity<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
       logger.error("全局异常，参数解析失败 : ", e);
       return new ResponseEntity<>(new ResponseBaseModel(29012,"参数解析失败"), HttpStatus.OK);
   }
   
   /**
    * 405 - Method Not Allowed
    */
   @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
   @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
   public @ResponseBody ResponseEntity<?> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
       logger.error("全局异常，不支持当前请求方法", e);
       return new ResponseEntity<>(new ResponseBaseModel(29013,"不支持当前请求方法"), HttpStatus.OK);
   }

   /**
    * 415 - Unsupported Media Type
    */
   @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
   @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
   public @ResponseBody ResponseEntity<?> handleHttpMediaTypeNotSupportedException(Exception e) {
       logger.error("全局异常，不支持当前媒体类型", e);
       return new ResponseEntity<>(new ResponseBaseModel(29014,"不支持当前媒体类型"), HttpStatus.OK);
   }

   /**
    * 500 - Internal Server Error
    */
   @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
   @ExceptionHandler(Exception.class)
   public @ResponseBody ResponseEntity<?> handleException(Exception e) {
//       if (e instanceof BusinessException){
//           return ServiceResponseHandle.failed("BUSINESS_ERROR", e.getMessage());
//       }
       
       logger.error("服务运行异常", e);
       return new ResponseEntity<>(new ResponseBaseModel(29015,"服务异常"), HttpStatus.OK);
   }
   
	

}
